package com.mdh.EightStudy;

import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

/**
 * @author mac
 * @date 2021/9/3 3:40 下午
 */
public class JavaBaseStudy {


    public static void main(String[] args) {
//        JavaBaseDataOperation.noImplement();
//        JavaBaseDataOperation.implement();

        JavaBaseDataOperation javaBaseDataOperation = new JavaBaseDataOperation();
        javaBaseDataOperation.test();
    }
}

/**
 * 知识点：equals和hashcode
 */
class JavaBaseDataOperation extends Date {
    private static final long serialVersionUID = 5310405918613019360L;
    private static  JavaBaseDataNoOverWriteEqualsMethod javaBaseDataNoOverWriteEqualsMethod = new JavaBaseDataNoOverWriteEqualsMethod();
    private static  JavaBaseDataOverWriteEqualsMethod javaBaseDataOverWriteEqualsMethod = new JavaBaseDataOverWriteEqualsMethod();
    static {
        javaBaseDataNoOverWriteEqualsMethod.setName("中文");
        javaBaseDataOverWriteEqualsMethod.setName("中文");
    }

    public void test(){
        //通过当前类的对象this获取类名称 通过父类对象super调用getClass()获取类名称 都是获取当前类的名称，后者不是获取父类名称的方法
        //原因：getClass()在Object中是final修饰的，子类不能重写，子类调用的这个方法是从父类直接继承过来的
        boolean equals = this.getClass().getName().equals(super.getClass().getName());
        System.out.println("equals = " + equals);
        //想要获取父类名称可以这么做
        String name = super.getClass().getSuperclass().getName();
        System.out.println("name = " + name);
    }
    /**
     * 没有重写hashcode和equals的测试
     */
    public static void noImplement(){
        MyBody[] values = MyBody.values();
        for (MyBody value : values) {
            int ordinal = value.ordinal();
            System.out.println("ordinal = " + ordinal);
        }
        JavaBaseDataNoOverWriteEqualsMethod javaBaseDataNoOverWriteEqualsMethod = new JavaBaseDataNoOverWriteEqualsMethod("中文");
        boolean equals = javaBaseDataNoOverWriteEqualsMethod.equals(javaBaseDataNoOverWriteEqualsMethod);
        System.out.println("equals = " + equals);
    }

    /**
     * 重写hashcode和equals的测试
     */
    public static void implement(){
        JavaBaseDataOverWriteEqualsMethod javaBaseDataOverWriteEqualsMethod = new JavaBaseDataOverWriteEqualsMethod("中文");
        boolean equals = javaBaseDataOverWriteEqualsMethod.equals(javaBaseDataOverWriteEqualsMethod);
        System.out.println("equals = " + equals);
    }
}

class JavaBaseDataNoOverWriteEqualsMethod implements Serializable {
    private static final long serialVersionUID = 2948192357954101603L;
    String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public JavaBaseDataNoOverWriteEqualsMethod() {
    }

    public JavaBaseDataNoOverWriteEqualsMethod(String name) {
        this.name = name;
    }
}

class JavaBaseDataOverWriteEqualsMethod extends JavaBaseDataNoOverWriteEqualsMethod{
    private static final long serialVersionUID = -8580977411324714294L;

    public JavaBaseDataOverWriteEqualsMethod(String name) {
        super(name);
    }

    public JavaBaseDataOverWriteEqualsMethod() {
    }

    @Override
    public boolean equals(Object obj) {
        return JavaBaseDataOverWriteEqualsMethod.class == obj.getClass()
                && obj instanceof JavaBaseDataOverWriteEqualsMethod
                && ((JavaBaseDataOverWriteEqualsMethod) obj).getName().equals(this.getName());
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}

enum MyBody{
    HEAD("头","1"),
    BODY("躯干","2"),
    LEG("腿","3")
    ;
    private String name;
    private String no;

    public void setName(String name) {
        this.name = name;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public String getNo() {
        return no;
    }

    MyBody(String name, String no) {
        this.name = name;
        this.no = no;
    }
}